﻿/*
求逆序数对 
Time Limit:1000MS  Memory Limit:32768K


Description:
逆序数定义如下：在序列S中，若iA[j]，则称A[i]与A[j]为S中的一对逆序数。

Input:
第一行给定整数N（2≤N≤50），下一行N个整数（没有重复，N=0时为输入结束）。 
Output:
输出每组序列的逆序数对。 
Sample Input:
3
1 2 3
3
3 2 1
3
1 3 2
0
Sample Output:
0
3
1
*/
/*
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct Elem
{
	int value;
	unsigned index;
	Elem(int value_, unsigned index_) : value(value_), index(index_)
	{
	}
	friend bool compare(const Elem& e1, const Elem& e2)
	{
		return e1.value < e2.value;
	}
};
int main()
{
	for (unsigned n; cin >> n && n > 1;)
	{
		vector<Elem> v;
		v.reserve(n + 1);
		{
			for (unsigned i = 1; i <= n; ++i)
			{
				int value;
				cin >> value;
				v.push_back(Elem(value, i));
			}
		}


		sort(v.begin(), v.end(), compare);
		unsigned inverse = 0u;
		for (unsigned i = 1, size=v.size(); i <= size; ++i)
		{
			if (i > v[i-1].index)
				inverse += i - v[i-1].index;
		}
// 		if(n&0x1U)
// 			++inverse;
		cout << inverse << endl;
	}
	return EXIT_SUCCESS;
}
*/

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdlib>
using namespace std;
int main()
{
	for (unsigned n; cin >> n && n > 1;)
	{
		unsigned inverse=0;
		
		vector<int> v;
		v.reserve(n+1);
		for (unsigned i=0; i< n; ++i)
		{
			unsigned num;
			cin>>num;
			v.push_back(num);
			vector<int>::iterator pos=lower_bound(v.begin(), v.end(), num);
			inverse+=(v.end()-pos-1);
			
			push_heap(v.begin(), v.end());
			make_heap(v.begin(), v.end());
			sort_heap(v.begin(), v.end());
		}
		cout<<inverse<<endl;
	}
	
	return EXIT_SUCCESS;
}